/**
 * Created by Ysssssss on 2021/5/18.
 */
import apiType, {
    GET_WECHAT_MINI_INFO,
    GET_WECHAT_MINI_TEST_QR_CODE,
    GET_WECHAT_MINI_VERSION_LOGS,

    GET_WECOM_BIZ_BIND,
    SET_WECOM_BIZ_BIND,
    LIST_WECOM_CORP_SUITES,
    GET_WECOM_AUTHORIZE_SUITE_QR_CODE,

    GET_ALIPAY_MINI_INFO,
    GET_ALIPAY_MINI_TEST_QR_CODE,
    GET_ALIPAY_MINI_VERSION_LOGS,
    GET_BYTEDANCE_MINI_INFO,
    GET_BYTEDANCE_MINI_TEST_QR_CODE,
    GET_BYTEDANCE_MINI_VERSION_LOGS,
    CREATE_CUSTOMIZE_MINI_CODE,
} from "@constants/ActionTypes";

const initialState = {
    wechatMiniInfo: {},
    wechatQrCode: '',
    wechatVersionLogs: {},

    wecomBizBindInfo: {},
    wecomAuthorizedSuites: [],
    wecomNotAuthorizedSuites: [],
    wecomAuthorizeSuiteQrCode: '',

    alipayMiniInfo: {},
    alipayQrCode: '',
    alipayVersionLogs: {},
    bytedanceMiniInfo: {},
    bytedanceQrCode: '',
    bytedanceVersionLogs: {},
    customizeMiniCode: {}
}

let temporaryCustomizeMiniCode = {}

export default function update(state = initialState, action) {
    const { response, params, payload } = action
    switch (action.type) {
        case apiType(GET_WECHAT_MINI_INFO).success:
            return {
                ...state,
                wechatMiniInfo: response
            }
        case apiType(GET_WECHAT_MINI_INFO).failure:
            return {
                ...state,
                wechatMiniInfo: {}
            }
        case apiType(GET_WECHAT_MINI_TEST_QR_CODE).success:
            return {
                ...state,
                wechatQrCode: response,
            }
        case apiType(GET_WECHAT_MINI_VERSION_LOGS).success:
            return {
                ...state,
                wechatVersionLogs: {
                    ...response,
                    pageNo: params.pageNum
                }
            }
        case apiType(GET_WECOM_BIZ_BIND).success:
            return {
                ...state,
                wecomBizBindInfo: response
            }
        case apiType(GET_WECOM_BIZ_BIND).failure:
            return {
                ...state,
                wecomBizBindInfo: {}
            }
        case apiType(SET_WECOM_BIZ_BIND).success:
            return {
                ...state,
                wecomBizBindInfo: response
            }
        case apiType(SET_WECOM_BIZ_BIND).failure:
            return {
                ...state,
                wecomBizBindInfo: {}
            }
        case apiType(LIST_WECOM_CORP_SUITES).success:
            return {
                ...state,
                wecomAuthorizedSuites: response.authorized_suites,
                wecomNotAuthorizedSuites: response.not_authorized_suites,
            }
        case apiType(LIST_WECOM_CORP_SUITES).failure:
            return {
                ...state,
                wecomAuthorizedSuites: [],
                wecomNotAuthorizedSuites: [],
            }
        case apiType(GET_WECOM_AUTHORIZE_SUITE_QR_CODE).success:
            return {
                ...state,
                wecomAuthorizeSuiteQrCode: response,
            }
        case apiType(GET_WECOM_AUTHORIZE_SUITE_QR_CODE).failure:
            return {
                ...state,
                wecomAuthorizeSuiteQrCode: '',
            }
        case apiType(GET_ALIPAY_MINI_INFO).failure:
            return {
                ...state,
                alipayMiniInfo: {}
            }
        case apiType(GET_ALIPAY_MINI_INFO).success:
            return {
                ...state,
                alipayMiniInfo: response
            }
        case apiType(GET_ALIPAY_MINI_TEST_QR_CODE).success:
            return {
                ...state,
                alipayQrCode: response,
            }
        case apiType(GET_ALIPAY_MINI_VERSION_LOGS).success:
            return {
                ...state,
                alipayVersionLogs: {
                    ...response,
                    pageNo: params.pageNum
                }
            }
        case apiType(GET_BYTEDANCE_MINI_INFO).success:
            return {
                ...state,
                bytedanceMiniInfo: response
            }
        case apiType(GET_BYTEDANCE_MINI_INFO).failure:
            return {
                ...state,
                bytedanceMiniInfo: {}
            }
        case apiType(GET_BYTEDANCE_MINI_TEST_QR_CODE).success:
            return {
                ...state,
                bytedanceQrCode: response,
            }
        case apiType(GET_BYTEDANCE_MINI_VERSION_LOGS).success:
            return {
                ...state,
                bytedanceVersionLogs: {
                    ...response,
                    pageNo: params.pageNum
                }
            }
        case apiType(CREATE_CUSTOMIZE_MINI_CODE).request:
            temporaryCustomizeMiniCode[params.codeKey] = {
                finishedCount: 0
            }
            return state;
        case apiType(CREATE_CUSTOMIZE_MINI_CODE).success:
            temporaryCustomizeMiniCode[payload.codeKey][payload.platformId] = response
            temporaryCustomizeMiniCode[payload.codeKey].finishedCount ++
            if (temporaryCustomizeMiniCode[payload.codeKey].finishedCount === 3) {
                return {
                    ...state,
                    customizeMiniCode: {
                        ...state.customizeMiniCode,
                        [payload.codeKey]: temporaryCustomizeMiniCode[payload.codeKey]
                    }
                }
            }
            return state
        case apiType(CREATE_CUSTOMIZE_MINI_CODE).failure:
            temporaryCustomizeMiniCode[payload.codeKey][payload.platformId] = ''
            temporaryCustomizeMiniCode[payload.codeKey].finishedCount ++
            if (temporaryCustomizeMiniCode[payload.codeKey].finishedCount === 3) {
                return {
                    ...state,
                    customizeMiniCode: {
                        ...state.customizeMiniCode,
                        [payload.codeKey]: temporaryCustomizeMiniCode[payload.codeKey]
                    }
                }
            }
            return state
        default:
            return state
    }
}